All files / src/app/api/admin/promotions/[id]/toggle route.ts

0% Statements 0/55
100% Branches 0/0
0% Functions 0/1
0% Lines 0/55

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56                                                                                                               
export const dynamic = "force-dynamic";

import { NextRequest, NextResponse } from 'next/server';
import { } from "next-auth";
import { prisma } from "@/lib/prisma";
import { logger } from "@/lib/logging";
import {
  withAdmin,
  withErrorHandling,
  successResponse,
  ApiError,
  ApiSuccessResponse,
  ApiErrorResponse } from "@/lib/api";
import { RouteContext } from "@/lib/api/middleware";

interface RouteParams {
  params: Promise<{ id: string }>;
}

/**
 * POST /api/admin/promotions/[id]/toggle
 * Toggle promotion active status
 */
async function handlePost(_request: NextRequest,
  context: RouteContext | undefined): Promise<NextResponse<ApiSuccessResponse<unknown> | ApiErrorResponse>> {
  const { id } = await (context as RouteParams).params;
  const promotionId = parseInt(id);

  if (isNaN(promotionId)) {
    throw ApiError.badRequest("Invalid promotion ID");
  }

  // Get current promotion
  const promotion = await prisma.promotion.findUnique({
    where: { id: promotionId } });

  if (!promotion) {
    throw ApiError.notFound("Promotion");
  }

  // Toggle status
  const updated = await prisma.promotion.update({
    where: { id: promotionId },
    data: { isActive: !promotion.isActive } });

  logger.info("Promotion toggled", { category: 'API', promotionId, name: promotion.name, isActive: updated.isActive });

  return successResponse({
    id: updated.id,
    name: updated.name,
    isActive: updated.isActive,
    message: updated.isActive ? "Promotion activated" : "Promotion deactivated" });
}

export const POST = withErrorHandling(withAdmin(handlePost));